package com.myaihui.controller;

import com.myaihui.domain.Resource;
import com.myaihui.domain.Role;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.msg.MsgDefinition;
import com.myaihui.foundation.msg.ResultMsg;
import com.myaihui.foundation.query.RoleQuery;
import com.myaihui.service.ResourceService;
import com.myaihui.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by sun on 2018/7/3.
 *
 * @author sunfuchang03@126.com
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/roles")
public class RoleController {
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Static fields/constants/initializer
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    private static Logger log = LoggerFactory.getLogger(RoleController.class);

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Instance fields
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Constructors
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */



    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Public Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping("/home")
    public String home(Model model) {
        return "role/home";
    }

    @GetMapping("/add")
    public String addPage(Model model) {
        List<Resource> resources = resourceService.findAllAvailable();
        model.addAttribute("resources", resources);
        return "role/add";
    }

    @GetMapping("/{roleId}/edit")
    public String editPage(@PathVariable String roleId, Model model) {
        List<Resource> resources = roleService.findResourcesByRoleId(roleId);
        List<Resource> allResources = resourceService.findAllAvailable();
        for (Resource allResource : allResources) {
            for (Resource resource : resources) {
                if (resource.getId().equals(allResource.getId())) {
                    allResource.setChecked(true);
                    break;
                }
            }
        }
        Role role = roleService.getById(roleId);
        model.addAttribute("resources", allResources);
        model.addAttribute("role", role);
        return "role/edit";
    }

    @PostMapping("/exists")
    @ResponseBody
    public ResultMsg checkNameExists(String roleId, String name) {
        if (!StringUtils.hasLength(name)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        boolean exists = false;
        if (StringUtils.hasLength(roleId)) {
            exists = roleService.getByNameExcludeId(name, roleId) != null;
        } else {
            exists = roleService.getByName(name) != null;
        }
        return ResultMsg.success(exists);
    }

    @RequestMapping("/{id}/delete")
    @ResponseBody
    public ResultMsg delete(@PathVariable("id") String id) {
        roleService.deleteWithResources(id);
        return ResultMsg.success(true);
    }

    @PostMapping("/save")
    @ResponseBody
    public ResultMsg update(Role role, String resIds) {
        roleService.saveRoleWithResources(role, StringUtils.hasLength(resIds) ? resIds.split(",") : null);
        return ResultMsg.success(true);
    }

    @PostMapping("/{id}/enable")
    @ResponseBody
    public ResultMsg enable(@PathVariable("id") String id, boolean enable) {
        Role role = roleService.getById(id);
        role.setAvailable(enable);
        roleService.update(role);
        return ResultMsg.success(true);
    }

    @PostMapping("/page")
    @ResponseBody
    public ResultMsg pageQuery(RoleQuery query) {
        Pager<Role> rolePager = roleService.pageQuery(query);
        return ResultMsg.success(rolePager);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Private Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */


}